from django import template
from django.core.cache import cache
from miles8.blog.models import *

register = template.Library()

@register.inclusion_tag('blog/category_snippet.html')   
def show_categorys():
    if not cache.get('categorys'):
        cache.set('categorys', Category.objects.all(), 60 * 5)
        
    categorys = cache.get('categorys')    
    return {'categorys': categorys}

@register.inclusion_tag('blog/archive_snippet.html')
def show_archive():
    if not cache.get('archive_dates'):
        cache.set('archive_dates', Article.objects.dates('post_time', 'month', order='DESC'), 60 * 5)
        
    archive_dates = cache.get('archive_dates')    
    return {'archive_dates': archive_dates}

@register.inclusion_tag('blog/last_comment_snippet.html')
def show_last_comment():
    comments = Comment.objects.all()[:10]
    
    return {'comments': comments}

@register.inclusion_tag('blog/last_article_snippet.html')
def show_last_article():
    articles = Article.objects.values('id', 'title')[:10]
    
    return {'articles': articles}